// JavaScript Document
dojo.require("com.otupia.ModuleLoader");
dojo.provide("com.otupia.moduleManager");
com.otupia.moduleManager = 
{
	LAODED : 0,
	NOT_EXIST : 1,
	LAODING : 2,
	LAODFAILED : 3,
	_registry : {},
	defferList:{},
	register : function(module)
	{
		this._registry[module.name] = module;
		this._registry[module.name].status = this.LAODED;
		for(p in this.defferList)
		{
			if(this.defferList[p] && this.defferList[p].who == module.name)
			{
		
				var _who = this.defferList[p].who;
				var _command = this.defferList[p].command;
				this._registry[_who][_command.method](_command.arguments);	
				delete (this.defferList[p]);
				
			}
		}
		return module.name;
		
	},			
	/**
	 * 调用模块的方法
	 */
	call: function(who,command,callerInfo)
	{
		if(this.getStatus(this._registry[who]) == this.LAODED)
		{
			if(this._registry[who][command.method] instanceof Function)
			{
				this._registry[who][command.method](command.arguments);	
			}
		}
		else
		{
			this.defferList[callerInfo.taskKey] = 
			{
				who : who,
				command : command,
				callerInfo : callerInfo
			};
			
			this._registry[who] = {status : this.LAODING};
			this.loader.load(callerInfo.caller,who);
		}
	},
	getStatus : function(name)
	{
		if(!this._registry[name] || !this._registry[name].status)
		{
			return this.NOT_EXIST;
		}
		else
		{
			return this._registry[name].status;
		}
	},
	getModule : function(name)
	{
		return this._registry[name];
	}
};
com.otupia.moduleManager.loader = (function(manager){
	var tempFunction = dojo.hitch(manager,manager.register);
	return new com.otupia.ModuleLoader(tempFunction);
})(com.otupia.moduleManager);


